local wuqian = fk.CreateSkill{
  name = "lingling__wuqian",
}

Fk:loadTranslationTable{
  ["lingling__wuqian"] = "无前",
  [":lingling__wuqian"] = "游戏开始时，你获得一张【杀】并随机装备一张坐骑牌。出牌阶段开始时，你可以弃置一张牌，然后随机从牌堆、弃牌堆、其他角色场上获得一张坐骑牌并使用，若顶替了原坐骑牌，"..
  "你视为使用一张无距离限制的【杀】。若你装备区有：<br>"..
  "+1马：<br>"..
  "【骅骝】，你使用【杀】伤害+1；<br>"..
  "【绝影】，你使用【杀】后摸一张牌；<br>"..
  "【爪黄飞电】，你使用【杀】指定目标后可以弃置其一张牌；<br>"..
  "【的卢】，你使用【杀】对其他角色造成伤害后，你可以弃置此牌，令其摸两张牌并翻面；<br>"..
  "-1马：<br>"..
  "【大宛】，你使用【杀】无法抵消；<br>"..
  "【紫骍】，你使用【杀】无次数限制；<br>"..
  "【赤兔】，你使用【杀】可以额外指定一名目标。",

  ["#lingling__wuqian-invoke"] = "无前：你可以弃置一张牌，随机获得一张坐骑牌并使用之",
  ["#lingling__wuqian-slash"] = "无前：你可以视为使用一张无距离限制的【杀】",
  ["#lingling__wuqian-zhuahuangfeidian"] = "无前：是否弃置 %dest 一张牌？",
  ["#lingling__wuqian-discard"] = "无前：弃置 %dest 一张牌",
  ["#lingling__wuqian-dilu"] = "无前：是否弃置【的卢】，令 %dest 摸两张牌并翻面？",
  ["@@lingling__wuqian_tip_hualiu"] = "杀伤害+1",
  ["@@lingling__wuqian_tip_jueying"] = "使用杀摸牌",
  ["@@lingling__wuqian_tip_zhuahuangfeidian"] = "杀弃置目标牌",
  ["@@lingling__wuqian_tip_dilu"] = "杀造成伤害翻面",
  ["@@lingling__wuqian_tip_dayuan"] = "杀无法抵消",
  ["@@lingling__wuqian_tip_zixing"] = "杀无次数限制",
  ["@@lingling__wuqian_tip_chitu"] = "杀额外指定目标",
}

wuqian:addEffect(fk.GameStart,{
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(wuqian.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local card = room:getCardsFromPileByRule("slash",1,"allPiles")
    if #card > 0 then
      room:obtainCard(player, card, true, fk.ReasonJustMove, player, wuqian.name)
    end
    local cards = table.filter(room.draw_pile, function (id)
        local card = Fk:getCardById(id)
        return (card.sub_type == Card.SubtypeOffensiveRide or card.sub_type == Card.SubtypeDefensiveRide) and player:canUseTo(card, player)
    end)
    if #cards > 0 then
      local card = Fk:getCardById(table.random(cards))
      room:useCard{
        from = player,
        tos = {player},
        card = card,
      }
    end
  end,
})
wuqian:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(wuqian.name) and player.phase == Player.Play and
      not player:isNude()
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards = room:askToDiscard(player, {
      min_num = 1,
      max_num = 1,
      include_equip = true,
      skill_name = wuqian.name,
      prompt = "#lingling__wuqian-invoke",
      cancelable = true,
      skip = true,
    })
    if #cards > 0 then
      event:setCostData(self, {cards = cards})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:throwCard(event:getCostData(self).cards, wuqian.name, player, player)
    if player.dead then return end
    local cards = {}
    for _, id in ipairs(table.connect(room.draw_pile, room.discard_pile)) do
      local card = Fk:getCardById(id)
      if card.sub_type == Card.SubtypeOffensiveRide or card.sub_type == Card.SubtypeDefensiveRide then
        table.insertIfNeed(cards, id)
      end
    end
    for _, p in ipairs(room:getOtherPlayers(player, false)) do
      for _, id in ipairs(p:getCardIds("e")) do
        local card = Fk:getCardById(id)
        if card.sub_type == Card.SubtypeOffensiveRide or card.sub_type == Card.SubtypeDefensiveRide then
          table.insertIfNeed(cards, id)
        end
      end
    end
    if #cards == 0 then return end
    local card = table.random(cards)
    room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonPrey, wuqian.name, nil, true, player)
    if player.dead or not table.contains(player:getCardIds("h"), card) then return end
    card = Fk:getCardById(card)
    if not player:canUse(card) then return end
    local yes = table.find(player:getCardIds("e"), function (id)
      local c = Fk:getCardById(id)
      return c.sub_type == card.sub_type and not player:hasEmptyEquipSlot(card.sub_type)
    end)
    room:useCard{
      from = player,
      tos = {player},
      card = card,
    }
    if yes and not player.dead then
      room:askToUseVirtualCard(player, {
        name = "slash",
        skill_name = wuqian.name,
        prompt = "#lingling__wuqian-slash",
        cancelable = true,
        extra_data = {
          bypass_distances = true,
          bypass_times = true,
          extraUse = true,
        },
      })
    end
  end,
})

wuqian:addEffect(fk.AfterCardUseDeclared, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(wuqian.name) and
      data.card.trueName == "slash" and
      table.find(player:getCardIds("e"), function (id)
        return Fk:getCardById(id).trueName == "hualiu"
      end)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    data.additionalDamage = (data.additionalDamage or 0) + 1
  end,
})

wuqian:addEffect(fk.CardUseFinished, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(wuqian.name) and
      data.card.trueName == "slash" and
      table.find(player:getCardIds("e"), function (id)
        return Fk:getCardById(id).trueName == "jueying"
      end)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player:drawCards(1, wuqian.name)
  end,
})

wuqian:addEffect(fk.TargetSpecified, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(wuqian.name) and
      data.card.trueName == "slash" and
      table.find(player:getCardIds("e"), function (id)
        return Fk:getCardById(id).trueName == "zhuahuangfeidian"
      end) and not data.to:isNude()
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {
      skill_name = wuqian.name,
      prompt = "#lingling__wuqian-zhuahuangfeidian::"..data.to.id,
    }) then
      event:setCostData(self, {tos = {data.to}})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local card = room:askToChooseCard(player, {
      target = data.to,
      flag = "he",
      skill_name = wuqian.name,
    })
    room:throwCard(card, wuqian.name, data.to, player)
  end,
})

wuqian:addEffect(fk.Damage, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(wuqian.name) and
      data.card and data.card.trueName == "slash" and
      table.find(player:getCardIds("e"), function (id)
        return Fk:getCardById(id).trueName == "dilu"
      end) and data.to ~= player and not data.to.dead
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {
      skill_name = wuqian.name,
      prompt = "#lingling__wuqian-dilu::"..data.to.id,
    }) then
      event:setCostData(self, {tos = {data.to}})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local card = table.find(player:getCardIds("e"), function (id)
      return Fk:getCardById(id).trueName == "dilu" and not player:prohibitDiscard(id)
    end)
    if not card then return end
    room:throwCard(card, wuqian.name, player, player)
    if data.to.dead then return end
    data.to:drawCards(2, wuqian.name)
    if data.to.dead then return end
    data.to:turnOver()
  end,
})

wuqian:addEffect(fk.CardUsing, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(wuqian.name) and
      data.card.trueName == "slash" and
      table.find(player:getCardIds("e"), function (id)
        return Fk:getCardById(id).trueName == "dayuan"
      end)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    data.unoffsetableList = table.simpleClone(player.room.players)
  end,
})

wuqian:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    if player:hasSkill(wuqian.name, true) then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerEquip then
              return true
            end
          end
        end
        if move.to == player and move.toArea == Card.PlayerEquip then
          return true
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local names = {
      "hualiu", "jueying", "zhuahuangfeidian", "dilu",
      "dayuan", "zixing", "chitu",
    }
    for _, name in ipairs(names) do
      room:setPlayerMark(player, "@@lingling__wuqian_tip_"..name, 0)
    end
    local cards = table.connect(player:getEquipments(Card.SubtypeOffensiveRide), player:getEquipments(Card.SubtypeDefensiveRide))
    for _, id in ipairs(cards) do
      local name = Fk:getCardById(id).trueName
      if table.contains(names, name) then
        room:setPlayerMark(player, "@@lingling__wuqian_tip_"..name, 1)
      end
    end
  end,
})

wuqian:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card)
    return player:hasSkill(wuqian.name) and card and card.trueName == "slash" and
      table.find(player:getCardIds("e"), function (id)
        return Fk:getCardById(id).trueName == "zixing"
      end)
  end,
  extra_target_func = function(self, player, skill, card)
    if player:hasSkill(wuqian.name) and card and card.trueName == "slash" and
      table.find(player:getCardIds("e"), function (id)
        return Fk:getCardById(id).trueName == "chitu"
      end) then
      return 1
    end
  end,
})

wuqian:addLoseEffect(function (self, player, is_death)
  local room = player.room
  local names = {
    "hualiu", "jueying", "zhuahuangfeidian", "dilu",
    "dayuan", "zixing", "chitu",
  }
  for _, name in ipairs(names) do
    room:setPlayerMark(player, "@@lingling__wuqian_tip_"..name, 0)
  end
end)

return wuqian
